package com.melody.android.ui.adapter

import android.annotation.SuppressLint
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.PopupMenu
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.melody.android.MelodyApplication
import com.melody.android.R
import com.melody.android.logic.model.SongList
import com.melody.android.logic.util.showToast
import com.melody.android.ui.activity.SongListActivity
import com.melody.android.ui.viewModel.SongViewModel

class SongListAdapter(private val songViewModel: SongViewModel, private val songLists: List<SongList>) : RecyclerView.Adapter<SongListAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val songListCover: ImageView = view.findViewById(R.id.img_song_list_cover)
        val songListName: TextView = view.findViewById(R.id.txt_song_list_name)
        val songNum: TextView = view.findViewById(R.id.txt_song_num)
        val imgMenuMore: ImageView = view.findViewById(R.id.img_menu_more)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SongListAdapter.ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_song_list, parent, false)
        val viewHolder = ViewHolder(view)
        viewHolder.itemView.setOnClickListener {
            val position = viewHolder.adapterPosition
            val songList = songLists[position]

            if (songList.num != 0) {
                val intent = Intent(parent.context, SongListActivity::class.java)
                intent.putExtra("songListDescription", songList.name)
                parent.context.startActivity(intent)
            } else {
                "当前歌单内没有歌曲".showToast()
            }
        }
        // 显示SongListMenu
        viewHolder.imgMenuMore.setOnClickListener {
            val position = viewHolder.adapterPosition
            showPopupMenu(it, position)
        }

        return viewHolder
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val songList = songLists[position]
        holder.songListCover.setImageResource(songList.cover)   // 自定义歌单的封面
        holder.songListName.text = songList.name                // 自定义歌单的名称
        holder.songNum.text = "${songList.num}首"            // 自定义歌单的歌曲数量
    }

    override fun getItemCount() = songLists.size

    private fun showPopupMenu(anchorView: View?, position: Int) {
        val popupMenu = PopupMenu(MelodyApplication.context, anchorView)
        popupMenu.menuInflater.inflate(R.menu.menu_song_list, popupMenu.menu)
        popupMenu.setOnMenuItemClickListener { item ->
            if (item.itemId == R.id.menu_delete) {
                // 执行删除操作
                val songList = songLists[position]
                songViewModel.removeSongList(songList)
                "删除成功".showToast()
                true
            } else {
                false
            }
        }
        popupMenu.show()
    }

}